package com.disdr.file;

import com.alibaba.nacos.common.utils.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.*;

public class 表格转图片 {

    public static void main(String[] args) throws IOException {
        //File file = new File("E:\\工资条测试版.xlsx");
        FileInputStream fileInputStream = new FileInputStream("E:\\工资条测试版.xlsx");
        XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
        Row headRow = sheet.getRow(0);

        // 获取工作表尺寸
        float w = 0;//sheet.getColumnWidth(0)/256*20+1 + sheet.getNumMergedRegions() * 2;
        for(int i=0;i<headRow.getLastCellNum();i++){
            //int www = sheet.getColumnWidth(i);
            //System.out.println(sheet.getColumnWidthInPixels(i));
            w = w + sheet.getColumnWidthInPixels(i);
        }
        Integer height = 0;
        for (int i=0; i<sheet.getPhysicalNumberOfRows(); i++) {
            height +=  (int)Math.ceil(sheet.getRow(i).getHeightInPoints());
        }

        int width = (int)Math.ceil(w);
        // 创建图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        // 设置背景颜色
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        //设置黑色画边框
        g2d.setColor(new Color(200, 200, 200));// 设置表格为灰色
        //g2d.setStroke(new BasicStroke(1.0f)); // 边框加粗
        g2d.drawRect(0, 0, width - 1, height - 1); // 画边框就是黑边框

        int x = 0;
        int y = 0;
        //绘制工作表
        //g2d.setStroke(new BasicStroke(0.0f)); // 边框不需要加粗
        for (int i=0; i<sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            y += (int)Math.ceil(row.getHeightInPoints());
            g2d.drawLine(0, y, width, y);
        }
        y = 0;
        for (int j=0; j<headRow.getLastCellNum(); j++) {
            x += sheet.getColumnWidthInPixels(j);
            g2d.drawLine(x, 0, x, height);
        }
        x=0;
        //填充数据
        for (int i=0; i<sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            y += (int)Math.ceil(row.getHeightInPoints());
            for (int j=0; j<row.getLastCellNum(); j++) {
                Cell cell = row.getCell(j);
                // 获取单元格数据
                String value = getCellString(cell);
                //获取单元格的样式
                CellStyle cellStyle = cell.getCellStyle();
                XSSFFont hssfFont = workbook.getFontAt(cellStyle.getFontIndexAsInt());
                if(hssfFont.getBold()){//字体加粗 + 字体大小
                    g2d.setFont(new java.awt.Font(null, Font.BOLD, hssfFont.getFontHeightInPoints()));
                }else{
                    g2d.setFont(new java.awt.Font(null, Font.PLAIN, hssfFont.getFontHeightInPoints()));
                }
                //字体颜色
                byte[] colors = hssfFont.getXSSFColor().getRGB();
                int r = colors[0]<0 ? colors[0]+256 : colors[0];
                int g = colors[1]<0 ? colors[1]+256 : colors[1];
                int b = colors[2]<0 ? colors[2]+256 : colors[2];
                g2d.setColor(new Color(r,g,b));

                //设置值
                g2d.drawString(value, x+10, y);
                x += sheet.getColumnWidthInPixels(j);
            }
            x = 0;
        }

        // 释放资源
        g2d.dispose();
        fileInputStream.close();

        // 输出图片
        ImageIO.write(image, "png", new File("E:\\output.png"));
    }

    public static String getCellString(Cell cell) {
        if (null != cell && StringUtils.isNotEmpty(cell.toString())) {
            if (cell.getCellType() == CellType.STRING) {
                return cell.getStringCellValue().trim();
            } else if (cell.getCellType() == CellType.NUMERIC) {
                //这种方法--数字编码要用它，金额带小数的的值会有精度丢失
                cell.setCellType(CellType.STRING);
                return cell.getStringCellValue().trim();
            } else {
                return cell.toString().trim();
            }
        }
        return "";
    }
}
